﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity.Validation;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Datos
{
    [MetadataType(typeof(ParametroMetaData))]
    public partial class Parametro {
        Repository<ArqParcial1Entities> repositorio = new Repository<ArqParcial1Entities>();
        ArqParcial1Entities context = new ArqParcial1Entities();

        public List<Parametro> Listar()
        {
            var result = from parametro in context.Parametros
                         select parametro;
            return result.ToList();
        }
        
        public Parametro Buscar(int iCodigo)
        {
            var result = from parametro in context.Parametros
                         where parametro.paraCodigo == iCodigo
                         select parametro;
            return result.Count()>0?result.First():new Parametro();
        }

        public Parametro Buscar(string sNombre)
        {
            var result = from parametro in context.Parametros
                         where parametro.paraNombre.Equals(sNombre)
                         select parametro;
            return result.Count()>0?result.First():new Parametro();
        }

        public List<Parametro> Consultar(int? iCodigo, string sNombre) {
            var query = from parametro in context.Parametros
                         select parametro;
            if (iCodigo == null && string.IsNullOrWhiteSpace(sNombre))
            {
                return new List<Parametro>();
            }
            else {
                if (iCodigo != null) {
                    query = query.Where(w => w.paraCodigo == iCodigo);
                }
                if (!string.IsNullOrWhiteSpace(sNombre)) {
                    query = query.Where(w => w.paraNombre.Contains(sNombre));
                }
                return query.ToList();
            }

        }

        public bool Guardar(Parametro parametro){
            using (System.Transactions.TransactionScope trn = new System.Transactions.TransactionScope())
            {
                try
                {
                    repositorio.Insert<Parametro>(parametro);
                    repositorio.Save();
                    trn.Complete();
                    return true;
                }
                catch (DbEntityValidationException e)
                {
                    foreach (var eve in e.EntityValidationErrors)
                    {
                        Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State);
                        foreach (var ve in eve.ValidationErrors)
                        {
                            Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage);
                        }
                    }
                    return false;

                }
            }
        }
        public bool Actualizar(Parametro parametro)
        {
            using (System.Transactions.TransactionScope trn = new System.Transactions.TransactionScope())
            {
                try
                {
                    repositorio.Update<Parametro>(parametro);
                    repositorio.Save();
                    trn.Complete();
                    return true;
                }
                catch (DbEntityValidationException e)
                {
                    foreach (var eve in e.EntityValidationErrors)
                    {
                        Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State);
                        foreach (var ve in eve.ValidationErrors)
                        {
                            Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage);
                        }
                    }
                    return false;

                }
            }
        }
        public bool Eliminar(Parametro parametro)
        {
            using (System.Transactions.TransactionScope trn = new System.Transactions.TransactionScope())
            {
                try
                {
                    repositorio.Delete<Parametro>(parametro);
                    repositorio.Save();
                    trn.Complete();
                    return true;
                }
                catch (DbEntityValidationException e)
                {
                    foreach (var eve in e.EntityValidationErrors)
                    {
                        Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State);
                        foreach (var ve in eve.ValidationErrors)
                        {
                            Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage);
                        }
                    }
                    return false;

                }
            }
        }
    }
    class ParametroMetaData
    {
        [DisplayName("Codigo")]
        public int parCodigo { get; set; }
        [DisplayName("Nombre")]
        public string parNombre { get; set; }
        [DisplayName("Descripcion")]
        public string parDescripcion { get; set; }
        [DisplayName("Valor")]
        public string parValor { get; set; }
    }
}
